import {KoaMiddlewareInterface,Middleware} from "routing-controllers";

@Middleware({type:"after"})
export class errorMiddleware implements KoaMiddlewareInterface {

  use(ctx: any, next: (err?: any) => Promise<any>): Promise<any> {
    return next().catch((err) => {
      if(err.status === 401){
        ctx.response.status = 401
        ctx.response.body = '未授权，访问被拒绝'
        ctx.status = 401
        return ctx.sendError('401', )
      }else{
        throw err
      }
    })
  }

}
